package jpa_objects;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;



/**
 * @author Alejandro Casas
 * @version 1.0
 * @created 10-Dec-2012 1:01:03 AM
 */
@Entity
public class Message  implements Comparable{

	private String date;
	private long id;
	private String mailFrom;
	private String mailTo;
	private String message;
	private String title;
	private boolean readbool;
	private String nameFrom;
	private String miniPictureFrom;

	

	/**
	 * @return the mailTo
	 */
	public String getMailTo() {
		return mailTo;
	}

	/**
	 * @param mailTo the mailTo to set
	 */
	public void setMailTo(String mailTo) {
		this.mailTo = mailTo;
	}

	/**
	 * @return the read
	 *
	public boolean isRead() {
		return read;
	}

	/**
	 * @param read the read to set
	 *
	public void setRead(boolean read) {
		this.read = read;
	}

	/**
	 * @return the date
	 */
	public String getDate() {
		return date;
	}

	/**
	 * @param date the date to set
	 */
	public void setDate(String date) {
		this.date = date;
	}

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the mailFrom
	 */
	public String getMailFrom() {
		return mailFrom;
	}

	/**
	 * @param mailFrom the mailFrom to set
	 */
	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	public Message(){

	}

	/**
	 * @param date
	 * @param mailFrom
	 * @param message
	 * @param title
	 */
	public Message(String date, String mailFrom, String message, String title,String mailTo,
			String nameFrom,String miniPictureFrom) {
		this.date = date;
		this.mailFrom = mailFrom;
		this.message = message;
		this.title = title;
		//this.read=false;
		this.mailTo=mailTo;
		this.nameFrom = nameFrom;
		this.miniPictureFrom=miniPictureFrom;
	}

	

	/**
	 * @return the readbool
	 */
	public boolean isReadbool() {
		return readbool;
	}

	/**
	 * @param readbool the readbool to set
	 */
	public void setReadbool(boolean readbool) {
		this.readbool = readbool;
	}

	/**
	 * @return the miniPictureFrom
	 */
	public String getMiniPictureFrom() {
		return miniPictureFrom;
	}

	/**
	 * @param miniPictureFrom the miniPictureFrom to set
	 */
	public void setMiniPictureFrom(String miniPictureFrom) {
		this.miniPictureFrom = miniPictureFrom;
	}

	public String getNameFrom() {
		return nameFrom;
	}

	public void setNameFrom(String nameFrom) {
		this.nameFrom = nameFrom;
	}

	/**
	 * 
	 * @param message
	 */
	public int compareTo(Message message){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
		
		try {
			java.util.Date mdated = dateFormat.parse(message.getDate());
			java.util.Date thisdated = dateFormat.parse(date);

			java.sql.Timestamp mdate = new java.sql.Timestamp(mdated.getTime());
			java.sql.Timestamp thisdate = new java.sql.Timestamp(thisdated.getTime());
			if(mdate.after(thisdate))
			{
				return -1;
			}
			else if(mdate.before(thisdate))
			{
				return 1;
			}
			else
			{
				return 0;
			}
		} catch (ParseException e) {
			// 
			e.printStackTrace();
			return 0;
		}
	}


	public int compareTo(Object o) {
		if( o instanceof Message)
		{
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
			
			try {
				java.util.Date mdated = dateFormat.parse(((Message)o).getDate());
				java.util.Date thisdated = dateFormat.parse(date);

				java.sql.Timestamp mdate = new java.sql.Timestamp(mdated.getTime());
				java.sql.Timestamp thisdate = new java.sql.Timestamp(thisdated.getTime());
				if(mdate.after(thisdate))
				{
					return -1;
				}
				else if(mdate.before(thisdate))
				{
					return 1;
				}
				else
				{
					return 0;
				}
			} catch (ParseException e) {
				// 
				e.printStackTrace();
				return 0;
			}
		}
		return 0;
	}

	


}